//
//  FJPushViewController.swift
//  FruitJuice
//
//  Created by Rui on 2023/12/18.
//

import UIKit
import ZLPhotoBrowser

class FJPushViewController: FJBaseViewController, SAAnimationProtocol {

    private let scrollView = UIScrollView()
    private let containerView = UIView()
    private let backButton = c_make_ex(UIButton.self)
        .image("FJ_Profile_back").end()
    private let cateView = FJPushCateView()
    private let pictureView = FJPushProductPicView()
    private let materialView = FJPushMaterialsView()
    private let otherView = FJPushOtherView()
    private let coinsView = FJPushCoinsView()
    private let noteView = FJPushNotesView()
    private let shareView = FJPushShareView()
    
    private var pushModel = FJPushModel()
    private var productUrl = [String]()
    private var fruitImg = [String]()
    private var greenImg = [String]()
    private var noteString = ""
    private var otherString = ""
    private var weightString = ""
    private var moneyString = ""
    

    override func viewDidLoad() {
        super.viewDidLoad()

        navigation.bar.isHidden = true
        view.subviews{
            scrollView.subviews{
                containerView.subviews{
                    cateView
                    pictureView
                    materialView
                    otherView
                    coinsView
                    noteView
                    shareView
                }
            }
            backButton
        }
        
        scrollView.leading(0).trailing(0).bottom(0).top(0)
        scrollView.contentInsetAdjustmentBehavior = .never
        containerView.fillContainer().width(.screenWidth)
        backButton.top(.statusHeight + 7).leading(15).size(30)
        cateView.leading(0).trailing(0).top(.navi_height).height(65)
        pictureView.leading(0).trailing(0).height(210)
        pictureView.Top == cateView.Bottom
        materialView.leading(0).trailing(0).height(190)
        materialView.Top == pictureView.Bottom
        otherView.leading(0).trailing(0).height(86)
        otherView.Top == materialView.Bottom
        coinsView.leading(0).trailing(0).height(86)
        coinsView.Top == otherView.Bottom
        noteView.leading(0).trailing(0).height(190)
        noteView.Top == coinsView.Bottom
        shareView.leading(0).trailing(0).height(50).bottom(CGFloat.safe_bottom + 35.0)
        shareView.Top == noteView.Bottom
        
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
        pictureView.picClosure = {
            ZLPhotoConfiguration.default()
                .allowSelectVideo(false)
            let ps = ZLPhotoPreviewSheet()
            ps.selectImageBlock = { [weak self] results, isOriginal in
                ToastTool.show()
                let image = results.first?.image
                RT.uploadCoverImage(file: (self?.imageToBase64String(image: image!))!) { [weak self] (dict) in
                    ToastTool.dismiss()
                    self?.productUrl.append(dict.url!)
                    let model = FJPushPicModel(url: dict.url!,id: 0, image: image)
                    self?.pictureView.dataSource.insert(model, at: 0)
                    self?.updateShareButtonState()
                }
            }
            ps.showPhotoLibrary(sender: self)
        }
        pictureView.delClosure = { indexId in
            self.productUrl.remove(at: indexId)
            self.updateShareButtonState()
        }
        materialView.fruitClosure = {
            let fruitView = FJSelMaterialsView()
            fruitView.type = 1
            self.saAnimate.animation(show: fruitView)
        }
        materialView.greenClosure = {
            let greenView = FJSelMaterialsView()
            greenView.type = 2
            self.saAnimate.animation(show: greenView)
        }
        otherView.closeClosure = {
            self.weightString = ""
            self.otherString = ""
            self.otherView.isHidden = true
            self.otherView.heightConstraint?.constant = 0
        }
        coinsView.closeClosure = {
            self.moneyString = ""
            self.coinsView.isHidden = true
            self.coinsView.heightConstraint?.constant = 0
        }
        coinsView.moneyClosure = { money in
            self.moneyString = money
        }
        otherView.otherClosure = { other in
            self.otherString = other
        }
        otherView.weightClosure = { weight in
            self.weightString = weight
        }
        noteView.noteClosure = { [weak self] text in
            self?.noteString = text
            self?.updateShareButtonState()
        }
        shareView.shareClosure = {
            
            self.pushModel.icon = self.productUrl.joined(separator: ",")
            self.pushModel.info = self.fruitImg.joined(separator: ",")
            self.pushModel.info0 = self.greenImg.joined(separator: ",")
            self.pushModel.descInfo = self.noteString
            self.pushModel.userId = USEINFOMODEL.id ?? ""
            self.pushModel.info2 = USEINFOMODEL.name ?? ""
            self.pushModel.info3 = USEINFOMODEL.headImage ?? ""
            let dateStr = Date().toString(.MonthDay)
            self.pushModel.info4 = dateStr
            if self.moneyString.count > 0 {
                self.pushModel.price = self.moneyString
            }else{
                self.pushModel.price = "0"
            }
            if self.otherString.count > 0 && self.weightString.count > 0 {
                self.pushModel.info1 = self.otherString + self.weightString + "(g)"
            }
            
            RT.createHouse(paraInfo: self.pushModel.kj.JSONObject()) { s in
                "Success!".toast_success
                self.navigationController?.popViewController(animated: true)
            }
        }
        
        NotificationCenter.default.addObserver(self, selector: #selector(updateFruitImgData), name: .updateFruit, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(updateGreenImgData), name: .updateFruit, object: nil)
    }
    
}

//MARK: action
extension FJPushViewController {
    
    private func updateShareButtonState(){
        
        if productUrl.count > 0 && fruitImg.count > 0 && greenImg.count > 0 && noteString.count > 0{
            shareView.buttonEnable()
        }else{
            shareView.buttonDisable()
        }
    }
    
    @objc private func updateFruitImgData(_ noti: Notification){
        
        if let imgArray = noti.userInfo?["data"] {
            let imgA = imgArray as! [String]
            fruitImg = imgA
            updateShareButtonState()
        }
    }
    
    @objc private func updateGreenImgData(_ noti: Notification){
        
        if let imgArray = noti.userInfo?["data"] {
            let imgA = imgArray as! [String]
            greenImg = imgA
            updateShareButtonState()
        }
    }
    
    @objc private func clickBackButton(){
        self.navigationController?.popViewController(animated: true)
    }
}
